﻿#include"stdafx.h"
#include"Struct.h"
#include<vector>
#include<string>
#include "CURDTools.h"
#include"util.h"

using namespace std;

bool HasAvilableIndex(vector<struct_where> wh, struct_where &result)
{
	if (wh.size() == 1 && wh[0].index) {
		result = wh[0];
		return true;
	}
	bool flag = true;
	for (unsigned int i = 0; i < wh.size(); i++)
		if (wh[i].joiner == "|")
			return false;
	for (unsigned int i = 0; i < wh.size(); i++)
		if (wh[i].index) {
			result = wh[i];
			return true;
		}
	return false;
}
///select的时候遍历文件
void Traversal(CFile & file, int offset, vector<insert_model> &fields, vector<struct_where>& where, string & info)
{
	if (MeetWhere(file,offset,where)) {
		for (unsigned i = 0; i < fields.size(); i++) {
			info += Recover(file,offset+fields[i].pos,fields[i].type,fields[i].mmSize);
		}
		info += "\r\n";
	}
}
//update 的时候遍历文件
void Traversal(CFile & file, int offset, vector<insert_model> &fields, vector<struct_where>& where)
{
	if (MeetWhere(file, offset, where)) {
		for (unsigned i = 0; i < fields.size(); i++) {
			updateInfo(file, offset + fields[i].pos,fields[i]);
		}
	}
}

void Traversal(CFile & file, CFile &temp_file,int offset, int lenth,vector<struct_where>& where)
{
	if (!MeetWhere(file, offset, where)) {
		char * content = new char[lenth + 1];
		memset(content, 0, lenth + 1);
		file.Seek(offset, file.begin);
		file.Read(content, lenth);
		temp_file.Write(content, lenth);
		delete content;
	}
}

bool MeetWhere(CFile&file, int offset, vector<struct_where>& where) {
	if(where.size()==0)
		return true;
	bool cur = false;
	bool flag = false;
	for (unsigned int i = 0; i < where.size(); i++) {
		cur = JudegWhere(file,offset+where[i].pos,where[i]);
		if (i == 0)
			flag = cur;
		else if (where[i - 1].joiner == "&") {
			flag = flag&cur;
		}
		else if (where[i - 1].joiner == "|") {
			flag = flag| cur;
		}
	}
	return flag;
}

string Recover(CFile & file, int offset, string type,int size)
{
	file.Seek(offset, file.begin);
	string result = "   ";
	if (type == "1") {
		int temp = 0;
		file.Read(&temp, sizeof(int));
		result = to_string(temp);
	}
	else if (type == "2") {
		bool temp = false;
		result = "false";
		file.Read(&temp, sizeof(bool));
		if (temp)
			result = "true";
		
	}
	else if (type == "3") {
		double temp = 0;
		file.Read(&temp, sizeof(double));
		result = to_string(temp);
	}
	else if (type == "4") {
		char temp[256] = { 0 };
		file.Read(temp, size);
		string t = temp;
		result = t;
	}
	else {
		unsigned long t = 0;
		file.Read(&t, sizeof(t));
		if (t == 0)
			result = "    ";
		else {
			CTime time(t);
			CString Cstime = time.Format("%Y-%m-%d");
			result = CStringTostring(Cstime);
		}
	}
	result += "  |  ";
	return result;
}

void updateInfo(CFile &file, int offset, insert_model &field) {
	string type = field.type;
	file.Seek(offset, file.begin);
	if (type == "1") {
		int temp = stoi(field.value);
		file.Write(&temp, sizeof(int));
	}
	else if (type == "2") {
		bool temp = false;
		if (field.value == "true")
			temp = true;
		file.Write(&temp, sizeof(bool));

	}
	else if (type == "3") {
		double temp = stod(field.value);
		file.Write(&temp, sizeof(double));
	}
	else if (type == "4") {
		 char temp[256] = { 0 };
		 Conversion(temp, field.value.c_str());
		file.Write(temp, field.mmSize);
	}
	else {
		unsigned long temp = 0;
		int nYear, nMonth, nDate, nHour = 0, nMin = 0, nSec = 0;
		sscanf(field.value.c_str(), "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
		CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
		unsigned long timeValue = (unsigned long)t.GetTime();
		//1495382400
		file.Write(&timeValue, 8);
	}
}